let http = require('http')

let server = http.createServer((req, res) => {
	// 通过http模块的STATUS_CODE方法，直接获取一个包含状态码和状态信息的对象
	// 获取对应状态码418的状态信息
	res.writeHead(418, http.STATUS_CODES["418"], {
		'Content-Type': 'text/html'
	});
	
	// 为http.ServerResponse对象绑定finish事件的监听
	res.addListener('finish', () => {
		console.log('server response is finished.');
	});
	
	res.end("<h1>Hello World!</h1>");
}).listen(8080, ()=>{
	console.log('http server starts at 8080 port');
});

// <http.Server> Extends: <net.Server>
// <net.Server> Extends: <EventEmitter>
// 既然最终 <http.Server> 构造函数继承自 <EventEmitter> 构造函数，那么根据原型链的原理，
// 它也可以调用 <EventEmitter> 的 emitter.on(eventName, listener) 方法。
server.on('connection', () => {
	console.log('a client has connected to the server.');
});

